package com.bobo.netty.service.impl;

import com.bobo.netty.base.ResponseResult;
import com.bobo.netty.base.ReturnCode;
import com.bobo.netty.common.ConstantsRedis;
import com.bobo.netty.dao.chat.ChatMessageMapper;
import com.bobo.netty.dao.chat.UserFriendMapper;
import com.bobo.netty.dao.chat.UserMapper;
import com.bobo.netty.dao.chat.po.UserFriendPO;
import com.bobo.netty.dao.chat.po.UserFriendPOExample;
import com.bobo.netty.dao.chat.po.UserPO;
import com.bobo.netty.dao.chat.po.UserPOExample;
import com.bobo.netty.service.UserService;
import com.bobo.netty.utils.IDUtils;
import com.bobo.netty.utils.RedisUtils;
import com.bobo.netty.vo.UserFriendVO;
import com.bobo.netty.vo.UserVO;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * yangbo
 * 2020-01-04
 * netty-actual-combat
 **/
@Service
public class UserServiceImpl implements UserService {
    private static final Logger LOGGER = LoggerFactory.getLogger(ChatServiceImpl.class);
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private ChatMessageMapper chatMessageMapper;
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserFriendMapper userFriendMapper;

    @Override
    public ResponseResult<String> registerUser(UserVO userVO) {
        UserPOExample example = new UserPOExample();
        example.createCriteria().andUserPhoneEqualTo(userVO.getPhone());
        List<UserPO> userPOS = userMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(userPOS)){
            return ResponseResult.error(ReturnCode.ERROR.getCode(), "手机号已存在", userVO.getUserName());
        }
        UserPO userPO = new UserPO();
        String uUid = IDUtils.getUUid();
        userPO.setUserId(uUid);
        userPO.setUserName(userVO.getUserName());
        userPO.setPassword(userVO.getPassWord());
        userPO.setUserPhone(userVO.getPhone());
        userMapper.insert(userPO);
        redisUtils.add(ConstantsRedis.USER_ID_PRE+uUid,userVO.getUserName());
        return ResponseResult.success(uUid);
    }

    @Override
    public ResponseResult<List<UserVO>> getAllUser(String userName) {
        UserPOExample example = new UserPOExample();
        if(StringUtils.isNotBlank(userName)){
            example.createCriteria().andUserNameLike(userName);
        }
        List<UserPO> userPOS = userMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(userPOS)){
            return ResponseResult.success(null);
        }
        List<UserVO> voList = new ArrayList<>();
        for (UserPO userPO:userPOS){
            UserVO userVO = new UserVO();
            userVO.setId(userPO.getUserId());
            userVO.setUserName(userPO.getUserName());
            userVO.setPhone(userPO.getUserPhone());
            voList.add(userVO);
        }
        return ResponseResult.success(voList);
    }

    @Override
    public ResponseResult<String> login(UserVO userVO) {
        UserPOExample example = new UserPOExample();
        example.createCriteria().andUserPhoneEqualTo(userVO.getPhone()).andPasswordEqualTo(userVO.getPassWord());
        List<UserPO> userPOS = userMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(userPOS)){
            return ResponseResult.error(ReturnCode.ERROR.getCode(), "用户名密码错误", userVO.getUserName());
        }else {
            UserPO userPO = userPOS.get(0);
            redisUtils.add(ConstantsRedis.USER_ID_PRE+userPO.getUserId(),userPO);
            return ResponseResult.success(userPO.getUserId());
        }

    }

    @Override
    public ResponseResult<String> addFriend(UserFriendVO userFriendVO) {
        UserFriendPO record = new UserFriendPO();
        record.setUserId(userFriendVO.getUserId());
        record.setFriendId(userFriendVO.getFriendId());
        record.setStatus(0);
        int insert = userFriendMapper.insert(record);
        LOGGER.info("insert="+insert+"id="+record.getId());
        return ResponseResult.success("添加成功");
    }

    @Override
    public ResponseResult<List<UserVO>> getMyFriendList(String id) {
        UserFriendPOExample example = new UserFriendPOExample();
        example.createCriteria().andUserIdEqualTo(id).andStatusEqualTo(0);
        List<UserFriendPO> userFriendPOS = userFriendMapper.selectByExample(example);
        if(CollectionUtils.isEmpty(userFriendPOS)){
            return ResponseResult.success(null);
        }
        List<UserVO> voList = new ArrayList<>();
        for (UserFriendPO userFriendPO:userFriendPOS){
            UserVO userVO = new UserVO();
            userVO.setId(userFriendPO.getUserId());
            voList.add(userVO);
        }
        return ResponseResult.success(voList);
    }

}
